<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./css/bootstrap.min.css">
</head>

    
<body>
    <!-- 给一个容器 -->
    <div class="container">
        <div class="row text-center">
            <h1>学生信息管理系统</h1>
            <hr>

            <a href="" class="btn btn-success">首页</a>
            <a href="./add.html" class="btn btn-warning">添加</a>

            <br>
            <br>
        </div>


        <div class="row">

            <table class="table table-bordered table-condensed table-hover table-responsive text-center" id="tbl">
            </table>    
           
        </div>
    </div>
    <script src="./js/jquery.min.js"></script>
    <script>
        
        $.ajax({
            url:'http://127.0.0.1:3000/index',
            type:'get',
            data:{},
            success:function(res){
                let str='';
                str+=`<tr>
                    <th>ID</th>
                    <th>姓名</th>
                    <th>年龄</th>
                    <th>性别</th>
                    <th>班级</th>
                    <th>成绩</th>
                    <th>操作</th>
                </tr>`;
                res.data.forEach((item,index) => {
                    str+=`<tr>
                    <td>${item.id}</td>
                    <td>${item.name}</td>
                    <td>${item.age}</td>
                    <td>${item.sex}</td>
                    <td>${item.class}</td>
                    <td>${item.score}</td>
                    <td>
                        <a href="#" class="btn btn-danger" id="del" tag="${index+1}">删除</a>
                        <a href="./edit.html#${index+1}" class="btn btn-warning">修改</a>
                    </td>
                </tr>`
                });
                $('#tbl').html(str);
            }
        });
        //   删除按钮事件
        $('#tbl').on('click','a',function(){
            console.log($(this).attr('tag'));
            let id=($(this).attr('tag'));
            $.ajax({
            url: 'http://127.0.0.1:3000/del',
            type: 'post',
            data: {
                id: id
            },
            success: function (res) {
                history.go(0)
            }
            }) 
        })
    </script>
</body>

</html>